package com.neuedu.controller;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.entity.EmploymentApplication;
import com.neuedu.entity.Questions;
import com.neuedu.entity.UmsAdmin;
import com.neuedu.service.QuestionsService;
import com.neuedu.vo.ResultJson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.stat.inference.BinomialTest;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/questions")
@Tag(name = "面试问题表")
public class QuestionsController
{
    @Resource
    QuestionsService questionsService;
    @PostMapping("/add")
    @Operation(summary = "新增问题")
    ResultJson<Boolean> add(@RequestBody Questions questions) {
        System.out.println(questions.toString());
        return ResultJson.success(questionsService.add(questions), "添加问题成功");
    }
    @PostMapping("/delete")
    @Operation(summary = "按照问题id删除问题")
    ResultJson<Boolean> delete(long id) {
        System.out.println(id+"a-----------------------------------------------------------");
        return ResultJson.success(questionsService.delete(id),  "删除成功");
    }

    @PostMapping("/update")
    @Operation(summary = "修改问题")
    ResultJson<Boolean> update(@RequestBody Questions questions) {
        return ResultJson.success(questionsService.update(questions), "修改成功");
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询")
    @Parameters({
            @Parameter(name = "pageNo", description = "页码"),
            @Parameter(name = "pageSize", description = "页大小")
    })
    ResultJson<IPage<Questions>> list(Integer pageNo, Integer pageSize) {
        return ResultJson.success(questionsService.page(new Page<>(pageNo, pageSize)));
    }
    @PostMapping("/preview")
    @Operation(summary = "预览题库")
    ResultJson<String> upload(MultipartFile file) throws IOException {
        String str = StreamUtils.copyToString(file.getInputStream(), StandardCharsets.UTF_8);
        return ResultJson.success(str);
    }
    @Operation(summary = "练习刷题")
    @Parameters({
            @Parameter(name = "count", description = "生成题目数量"),
    })
    @GetMapping("/practice")
    ResultJson<List<Questions>> practice(Integer count) {
        return ResultJson.success(questionsService.practice(count));
    }
    @PostMapping("/upload")
    @Operation(summary = "上传题库")
    ResultJson<Boolean> upload(String content) {
        return ResultJson.success(questionsService.upload(content), "导入成功");
    }

    @GetMapping("/getCount")
    ResultJson<Long> getCount() {
        return ResultJson.success(questionsService.count());
    }
}
